﻿/*
[]
*/

Ext.namespace("tk","tk.yunos","tk.yunos.system");

tk.yunos.system.WindowMgr = function(config){
	var config = config || {};
	var desktop = config.desktop;
	var bar = desktop.getBottomToolbar();
	var id = config.id || "global_window_mgr";
	var toggleGroup = "toggle_" + id;
	var windowMgr = this;
	
	var buttons = [];
	var windows = [];
	var activeWin;
	var shownWins = new YOSUtil.SingleArray();
	
	var registerBar = function(toolbar){
		if(toolbar && toolbar instanceof Ext.Toolbar){
			bar = toolbar;
			return true;
		}
		return false;
	};
	
	var register = function(window){
		if(window && window instanceof tk.yunos.system.Window){
			windows[window.getId()] = window;
			buttons[window.getId()] = new Ext.Button({
				text: window.title,
				enableToggle: true,
				toggleGroup: toggleGroup,
				scale: 'large',
				iconCls: window.getIconCls(),
				ctCls: 'x-btn-over',
				iconAlign: 'left',
				handler: function(b, e){
					if(activeWin == window){
						window.minimize();
					}else if(window.hidden){
						window.show();
					}else{
						window.sendFront();
					}
				},
				listeners: {
					'render': function(){
						window.animateTarget = this.el;
						this.el.on('contextmenu', function(event){
							event.preventDefault();
							var menu = new Ext.menu.Menu({
								items: [{
									text: '最大化',
									handler: function(){
										window.show();
										window.maximize();
									}
								},{
									text: '还原',
									handler: function(){
										window.show();
										window.restore();
									}
								},{
									text: '关闭窗口',
									handler: function(){
										window.close();
									}
								}],
								listeners: {
									'show': function(menu){
										var oldPosition = event.getXY();
										menu.setPosition(oldPosition[0], oldPosition[1] - menu.getHeight());
									}
								}
							});
							menu.showAt([0,0]);
						});
					}
				}
			});
			bar.add('-');
			bar.add(buttons[window.getId()]);
			bar.doLayout();
			return true;
		}
		return false;
	};
	
	var unRegister = function(window){
		if(window && window instanceof tk.yunos.system.Window && windows[window.getId()]){
			var sep = buttons[window.getId()].previousSibling();
			bar.remove(buttons[window.getId()]);
			if(sep){
				bar.remove(sep);
			}
			bar.doLayout();
			delete buttons[window.getId()];
			delete windows[window.getId()];
			return true;
		}
		return false;
	};
	
	var shiftWin = function(win){
		//删除关闭或最小化的window
		shownWins.del(win);
		activeWin = shownWins.get(shownWins.length() - 1);
		if(activeWin){
			activeWin.sendFront();
		}
	};
	
	var newWindow = function(config){
		var win = new tk.yunos.system.Window(config);
		win.NEW_FLAG = true;
		win.my_maximized = config.maximized || false;
		
		//最大化时调整高度以露出任务栏
		var adjustHeight = function(){
			win.setHeight(document.body.clientHeight - bar.getHeight() - 5);
		};
	
		//render时register
		win.addListener("render", function(){
			register(win);
			win.el.addListener("mousedown", function(){
				if(activeWin != win){
					win.fireEvent("show");
				}
			});
		});
		
		//close前unregister
		win.addListener("beforeclose", function(){
			unRegister(win);
			shiftWin(win);
		});
		
		//显示时toggle
		win.addListener("show", function(){
			if(win.maximized){
				adjustHeight();
			}else{
				if(win.NEW_FLAG){
					//新窗口调整位置
					var lastActiveWin = shownWins.length() > 0 ? shownWins.get(shownWins.length() - 1) : null;
					if(lastActiveWin != null && !lastActiveWin.my_maximized){
						var desktopWidth = desktop.getWidth();
						var desktopHeight = desktop.getHeight();
						var marginLeft = desktopWidth/20;
						var marginTop = win.header.getHeight() + 5;
						var lastActiveWinLeft = lastActiveWin.getPosition(true)[0];
						var lastActiveWinTop = lastActiveWin.getPosition(true)[1];
						if((desktopWidth - lastActiveWinLeft > win.getWidth())
							&& (desktopHeight - lastActiveWinTop > marginTop)){
							win.setPosition(lastActiveWin.getPosition(true)[0] + marginLeft, lastActiveWin.getPosition(true)[1] + marginTop);	
						}else{
							win.setPosition(marginLeft, marginTop);
						}
					}				
					win.NEW_FLAG = false;
				}
			}
			shownWins.push(win);
			activeWin = win;
			buttons[win.getId()].toggle(true);
		});
		
		//最小化时untoggle
		win.addListener("minimize", function(){
			win.hide();
			shiftWin(win);
			buttons[win.getId()].toggle(false);
		});
		
		//最大化时要调整高度以露出任务栏
		win.addListener("maximize", function(){
			adjustHeight();
			win.my_maximized = true;
		});
		
		win.addListener("titlechange", function(p, title){
			buttons[win.getId()].setText(title);
			buttons[win.getId()].ownerCt.doLayout();
		});
		
		return win;
	};
	
	var getBar = function(){
		return bar;
	};
	
	var getAllButtons = function(){
		return buttons;
	};
	
	var getButton = function(idorwindow){
		var button;
		if(idorwindow instanceof String){
			button = buttons[idorwindow];
		}else if(idorwindow instanceof tk.yunos.system.Window){
			button = buttons[idorwindow.getId()];
		}
		return button;
	};
	
	var getWindow = function(id){
		return windows[id];
	};
	
	var eachWindow = function(fn, scope){
		var _scope;
		for(index in windows){
			_scope = scope;
			if(!scope){
				_scope = windows[index];
			}
			fn.call(_scope, index, windows[index]);
		}
	};
	
	var getActive = function(){
		return activeWin;
	};
	
	var minimizeAll = function(){
		shownWins.clear();
		activeWin = undefined;
		for(index in windows){
			var win = windows[index];
			if(win.minimize){
				win.minimize();
			}
		}
	};
	
	var showAll = function(){
		for(index in windows){
			windows[index].show();
		}
	};
	
	return {
		registerBar:	registerBar,
		newWindow:		newWindow,
		getBar:			getBar,
		getAllButtons:	getAllButtons,
		getButton:		getButton,
		getWindow:		getWindow,
		eachWindow:		eachWindow,
		minimizeAll:	minimizeAll,
		showAll:		showAll
	};
};

tk.yunos.system.Window = function(config){
	var config = config || {};
	var win = this;
	var textTitle = config.title;
	var iconTitle = config.icon;
	var loadingImg = tk.yunos.filesystem.APP_DIR + '/tk.yunos.system/windows-manager/images/loading.gif';
	
	this.getIconCls = function(){
		return config.iconCls;
	};
	
	this.buildTitle = function(title, icon){
		if(!icon){
			icon = iconTitle;
		}
		if(icon && !config.iconCls){
			return '<img width="18px" height="18px" src="' + icon + '"></img>' + title;
		}
		return title;
	};
	
	this.sendFront = function(){
		win.toFront();
		win.fireEvent("show");
	};
	
	Ext.apply(this, config);
	tk.yunos.system.Window.superclass.constructor.call(this, config);
	
	win.setTitle = function(newTitle, newIcon){
		tk.yunos.system.Window.superclass.setTitle.call(win, win.buildTitle(newTitle, newIcon));
		textTitle = newTitle;
		if(newIcon && newIcon != loadingImg){
			iconTitle = newIcon;
		}
	};
	
	if(win.items){
		win.items.each(function(item){
			item.getWindow = function(){
				return win;
			};
		});
	}
	
	//等待加载
	var lastIcon = iconTitle;
	this.wait = function(){
		lastIcon = iconTitle;
		win.setTitle(textTitle, loadingImg);
	};
	//加载完毕
	this.finish = function(){
		win.setTitle(textTitle, lastIcon);
	};
};

Ext.extend(tk.yunos.system.Window, Ext.Window, {
	layout: 'fit',
	width: 450,
	height: 350,
	maximizable: true,
	minimizable: true,
	constrainHeader: true
});
